//
//  YSLScrollWebViewController.m
//  used
//
//  Created by 杨斯霖 on 16/7/17.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "YSLScrollWebViewController.h"

@interface YSLScrollWebViewController ()<UIWebViewDelegate>
@property (nonatomic, strong)UIWebView *webView;
/** 数据源 */
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation YSLScrollWebViewController

- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UIWebView *)webView {
    
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame: self.view.frame];
        _webView.delegate = self;
        _webView.scrollView.bounces = NO;
        _webView.scrollView.bouncesZoom = NO;
    }
    return _webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadData];
    [self.view addSubview: self.webView];
    
    //显示指示器
    [SVProgressHUD show];
    
    //捏合手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [self.webView addGestureRecognizer:pinch];
}

- (void)loadData
{
    //数据请求
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"typeId"] = @0;
    params[@"pageIndex"] = @1;
    [RequestManager fetchDataWithUrl:KHomeRecommendUrl parameter:@{@"page": @0} method:@"POST" compeleted:^(NSData *data) {
        
        //隐藏指示器
        [SVProgressHUD dismiss];
        
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        //添加轮播图的数据源
        for (NSDictionary *smallDic in bigDic[@"result"][@"carousel"]) {
            [self.dataSource addObject:smallDic[@"url"]];
            
        }
           [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.dataSource[0]]]];
        
    } updateUI:^{
        
        [self.webView reload];
    }];
}

#pragma mark ---webViewDelegate-----
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    NSString *js = @"function imgAutoFit() { \
    var s = document.getElementsByTagName('div'); \
    var j = s.length; \
    s[j-1].remove();   \
    }";

    js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];
    
    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];
}

#pragma mark - 手势
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
